package com.fjp.heartguard.ui.question.fragment

import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.RadioGroup.LayoutParams
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import com.fjp.heartguard.R
import com.fjp.heartguard.app.base.BaseFragment
import com.fjp.heartguard.app.data.bean.Questions
import com.fjp.heartguard.app.eventViewModel
import com.fjp.heartguard.app.ext.dp
import com.fjp.heartguard.databinding.FragmentSingleBinding
import com.fjp.heartguard.viewmodel.SingleSelectedViewModel


class SingleSelectedFragment : BaseFragment<SingleSelectedViewModel, FragmentSingleBinding>() {


    companion object {
        fun newInstance(questions: Questions): Fragment {
            val fragment = SingleSelectedFragment()
            val bundle = Bundle()
            bundle.putSerializable("question", questions)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewmodel = mViewModel
        val questions = arguments?.getSerializable("question") as Questions
        questions.let {
            mDatabind.questionTitleLayout.setType(questions.questionType)
            if("79、过去两周内是否去过医院急诊？" == it.questionTitle){
                val spannableString = SpannableString(it.questionTitle)
                spannableString.setSpan(ForegroundColorSpan(Color.RED), 14, 16, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                mDatabind.questionTv.text= spannableString
            }else if ("82、过去两周内是否进行过门诊就诊？" == it.questionTitle){
                val spannableString = SpannableString(it.questionTitle)
                spannableString.setSpan(ForegroundColorSpan(Color.RED), 13, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                mDatabind.questionTv.text= spannableString
            }else{
                mDatabind.questionTv.text = it.questionTitle
            }
            val group = RadioGroup(context)
            group.orientation = LinearLayout.VERTICAL
            it.options.forEachIndexed { index, options ->

                val rb = View.inflate(context, R.layout.radio_button, null) as RadioButton
                rb.text = options.title.replace("*", "")
                rb.isChecked = options.isSelected
                rb.id = index
                group.addView(rb)
                val editText:EditText
                if(options.title.contains("跌倒次数")|| options.title.contains("小时一次")){
                    editText = View.inflate(context, R.layout.edit_text_number, null) as EditText
                }else{
                    editText = View.inflate(context, R.layout.edit_text, null) as EditText
                }
                if (options.title.startsWith("*")) {
                    group.addView(editText)
                    val editTextLayoutParams = editText.layoutParams as RadioGroup.LayoutParams
                    editTextLayoutParams.setMargins(0, 16.dp.toInt(), 0, 0)
                    editTextLayoutParams.width = LayoutParams.MATCH_PARENT
                    editText.layoutParams = editTextLayoutParams

                    editText.setText(options.value)
                    if (rb.isChecked) {
                        editText.visibility = View.VISIBLE
                    } else {
                        editText.visibility = View.GONE
                    }
                    editText.addTextChangedListener {
                        options.value = it.toString()
                    }
                }
                rb.setOnCheckedChangeListener { buttonView, isChecked ->
                    run {
                        options.isSelected = isChecked
                        if (isChecked && options.title.startsWith("*")) {
                            editText.visibility = View.VISIBLE
                        } else {
                            editText.visibility = View.GONE
                        }

                        if(isChecked && !options.title.startsWith("*")){
                            eventViewModel.nextFragment.postValue(true)
                        }
                    }

                }
                val layoutParams = rb.layoutParams as RadioGroup.LayoutParams
                layoutParams.setMargins(0, 16.dp.toInt(), 0, 0)
                layoutParams.width = LayoutParams.MATCH_PARENT
                rb.layoutParams = layoutParams

            }
            mDatabind.optionContentLL.addView(group)
        }
    }

}